package com.qianyitian.hope2.analyzer.analyzer;

import com.qianyitian.hope2.analyzer.model.KLineInfo;
import com.qianyitian.hope2.analyzer.model.ResultInfo;
import com.qianyitian.hope2.analyzer.model.Stock;
import com.qianyitian.hope2.analyzer.util.AlgorithmUtil;

import java.util.List;

public class WeekVolumeAnalyzer extends AbstractStockAnalyzer {

    //成交量 是 前一周的 N 倍数
    //  0 < 本周涨幅 < M
    // 本周换手 > P
    // 收盘价 > 30均线

    private static final double N = 3.5;
    private static final double M = 0.1;
    private static final double P = 0.12;
    static String desTemplate = "本算法周四/周五使用,本周成交量是上周的%.1f倍,本周涨幅小于%.1f%%,本周换手大于%.1f%%,收盘价大于周30均价";

    public WeekVolumeAnalyzer() {
    }

    @Override
    public boolean analyze(ResultInfo resultInfo, Stock stock) {
        List<KLineInfo> infos = stock.getkLineInfos();
        if (infos.size() < 31) {
            return false;
        }

        KLineInfo today = infos.get(infos.size() - 1);
        KLineInfo yes = infos.get(infos.size() - 2);

        boolean ok = condition1(today, yes) && condition2(today, yes) && condition3(today, yes) && condition4(today, infos);
        if (!ok) {
            return false;
        }
        resultInfo.setValue(getTurnover(stock));
        return ok;
    }

    private boolean condition1(KLineInfo today, KLineInfo yes) {
        //成交量 是 前一周的 N 倍数
        return today.getTurnoverRate() / yes.getTurnoverRate() > N;
    }

    private boolean condition2(KLineInfo today, KLineInfo yes) {
        //  0 < 本周涨幅 < M
        double range = today.getClose() / yes.getClose() - 1;
        return range >= -0.05 && range < M;
    }

    private boolean condition3(KLineInfo today, KLineInfo yes) {
        // 本周换手 > P
        return today.getTurnoverRate() > P *100;
    }

    private boolean condition4(KLineInfo today, List<KLineInfo> list) {
        // 收盘价 > 30均线
        double ma30 = AlgorithmUtil.maN(list, 30);
        return today.getClose() >= ma30;
    }


    public String format(Stock stock) {
        return "";
    }

    @Override
    public String getDescription() {
        return String.format(desTemplate, N, M * 100, P * 100);
    }

    //成交量 是 前一周的 N 倍数
    //  0 < 本周涨幅 < M
    // 本周换手 > P
    // 收盘价 > 30均线
    public int getWeight() {
        return 150;
    }

}
